package com.fenghongzhang.kuaishou.mvp.login.model;

import io.reactivex.Observer;

import com.fenghongzhang.kuaishou.common.ShuaiFengServerApi;
import com.fenghongzhang.kuaishou.mvp.login.contract.LoginContract;
import com.fenghongzhang.kuaishou.mvp.login.entity.UserInfoBean;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class LoginModel implements LoginContract.ILoginModel {

    @Override
    public void login(String name, String pwd, Observer<UserInfoBean> observer) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(ShuaiFengServerApi.BASE_URL)
                .client(new OkHttpClient().newBuilder().addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)).build())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        ShuaiFengServerApi shuaiFengServerApi = retrofit.create(ShuaiFengServerApi.class);

        Observable<UserInfoBean> login = shuaiFengServerApi.login(name, pwd);
        login.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);

    }

    @Override
    public void destroy() {


    }
}
